﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Coding4Fun;
using Coding4Fun.Toolkit.Controls;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;

namespace JiveMessenger.Utilities
{

    public static class PromptHelper
    {
        private static ToastPrompt _toastPrompt;
        private static MessagePrompt _messagePrompt;
        private static InputPrompt _inputPrompt;

        public static void ShowInputPrompt(string title, string message, Action<string> onCompleted = null)
        {
            EventHandler<PopUpEventArgs<string, PopUpResult>> handler = null;
            handler = delegate(object sender, PopUpEventArgs<string, PopUpResult> e)
            {
                InputPrompt prompt = sender as InputPrompt;
                prompt.Completed -= handler;
                var action = prompt.Tag as Action<string>;
                if (action != null)
                {
                    if (action != null)
                        action(e.Result);
                }

            };
            if (_inputPrompt != null)
            {
                _inputPrompt.Completed -= handler;
                _inputPrompt.Tag = null;
                if (_inputPrompt.IsOpen)
                    _inputPrompt.Hide();
            }
            _inputPrompt = new InputPrompt
            {
                Message = message,
                Title = title,
                Tag = onCompleted,
            };
            _inputPrompt.Completed += handler;
            _inputPrompt.Show();
        }
        public static void ShowDialogPrompt(string title, string message, Action<PopUpResult> onCompleted = null)
        {
            EventHandler<PopUpEventArgs<string, PopUpResult>> handler = null;
            handler = delegate(object sender, PopUpEventArgs<string, PopUpResult> e)
            {
                MessagePrompt prompt = sender as MessagePrompt;
                prompt.Completed -= handler;
                var action = prompt.Tag as Action<PopUpResult>;
                if (action != null)
                {
                    if (action != null)
                        action(e.PopUpResult);
                }

            };
            if (_messagePrompt != null)
            {
                _messagePrompt.Completed -= handler;
                _messagePrompt.Tag = null;
                if (_messagePrompt.IsOpen)
                    _messagePrompt.Hide();
            }
            _messagePrompt = new MessagePrompt
            {
                Message = message,
                Title = title,
                Tag = onCompleted,
                 IsCancelVisible=true
            };

            _messagePrompt.Completed += handler;
            _messagePrompt.Show();
        }
        public static void ShowMessagePrompt(string title, string message, Action<PopUpResult> onCompleted = null)
        {
            EventHandler<PopUpEventArgs<string, PopUpResult>> handler = null;
            handler = delegate(object sender, PopUpEventArgs<string, PopUpResult> e)
            {
                MessagePrompt prompt = sender as MessagePrompt;
                prompt.Completed -= handler;
                var action = prompt.Tag as Action<PopUpResult>;
                if (action != null)
                {
                    if (action != null)
                        action(e.PopUpResult);
                }

            };
            if (_messagePrompt != null)
            {
                _messagePrompt.Completed -= handler;
                _messagePrompt.Tag = null;
                if (_messagePrompt.IsOpen)
                    _messagePrompt.Hide();
            }
            _messagePrompt = new MessagePrompt
            {
                Message = message,
                Title = title,
                Tag = onCompleted,
            };
          
            _messagePrompt.Completed += handler;
            _messagePrompt.Show();
        }


        public static void ShowToastPrompt(string title, string message, int delayTime = 2000, TextWrapping wrapMode = TextWrapping.NoWrap, Action onTapped = null)
        {

            EventHandler<PopUpEventArgs<string, PopUpResult>> handler = null;
            handler = delegate(object sender, PopUpEventArgs<string, PopUpResult> e)
            {
                ToastPrompt prompt = sender as ToastPrompt;
                prompt.Completed -= handler;
                var tuple = prompt.Tag as Tuple<Action, Action>;
                if (tuple != null)
                {
                    if (tuple.Item1 != null)
                        tuple.Item1();
                }

            };

            TappedEventHandler tappedHandler = null;
            tappedHandler = delegate(object sender, TappedRoutedEventArgs args)
            {
                ToastPrompt prompt = sender as ToastPrompt;
                prompt.Tapped -= tappedHandler;
                var tuple = prompt.Tag as Tuple<Action, Action>;
                if (tuple != null)
                {
                    if (tuple.Item2 != null)
                        tuple.Item2();
                }
                prompt.Hide();
            };

            if (_toastPrompt != null)
            {
                _toastPrompt.Completed -= handler;
                _toastPrompt.Tapped -= tappedHandler;
                _toastPrompt.Tag = null;
            }
            _toastPrompt = new ToastPrompt();
            _toastPrompt.Style = Application.Current.Resources["ToastPromptStyle"] as Style;
            _toastPrompt.TextWrapping = wrapMode;
            _toastPrompt.MillisecondsUntilHidden = delayTime;
            _toastPrompt.Title = title;
            _toastPrompt.Message = message;
            _toastPrompt.Completed += handler;
            _toastPrompt.Tag = new Tuple<Action, Action>(null, onTapped);
            _toastPrompt.Tapped += tappedHandler;
            _toastPrompt.Show();
        }



    }
}
